package com.zhudao.crawler.service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.Date;
import java.util.Map;

@Service
public class CrawlerFangTianXiaService {

    @Autowired
    @Qualifier("primaryJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    private static final Logger logger = LoggerFactory.getLogger(CrawlerFangTianXiaService.class);

    @Async
    public void fangtianxia(String p1, String p2) {
        while (true) {
            for (int i = 0; i < 100; i++) {
                try {
                    URL url = new URL("http://zu" + p1 + "fang.com/house/a21-i3" + i);
                    Document document = Jsoup.parse(url, 10000000);
                    Elements elements = document.select(".title a");
                    for (Element element : elements) {
                        String href = element.attr("href");
                        URL url2 = new URL("http://zu" + p1 + "fang.com" + href);
                        Thread.sleep(5000);
                        Document document2 = Jsoup.parse(url2, 1000000);
                        Elements elements2 = document2.select(".phoneicon");
                        Elements elements3 = document2.select(".name");
                        String phone = elements2.text();
                        String name = elements3.text();
                        logger.info("房天下,{},{},{}", name, phone, p2);
                        if (phone.indexOf("-") > 0) {
                            continue;
                        }
                        Map<String, Object> map1 = jdbcTemplate.queryForMap("SELECT COUNT(1) as count from userInfo where  phone=?", phone);
                        Integer count = Integer.valueOf(map1.get("count").toString());
                        if (count == 0) {
                            logger.info("插入房天下的数据，电话号码:{}", phone);
                            jdbcTemplate.update("INSERT into userInfo VALUE (?,?,?,?,?,?,?)", phone, name, "房东", p2, "", "房天下", new Date());
                        } else {
                            logger.info("房天下的数据，电话号码已存在{}", phone);
                        }
                    }
                } catch (Exception ex) {
                    logger.info("异常了，{}", ex.getMessage());
                }
            }
        }
    }
}